package lesson10;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求的编码格式
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        //拿到Ajax请求中携带的数据
        String name = request.getParameter("name");
        boolean used = false;
        String isUsed=null;
        String jsonData=null;//存储Json格式的数据
        if("ajax".equals(name)){
//            used = true;
//            isUsed="<h1>用户名已存在！</h1>";
            jsonData="{\"http://localhost:8080/untitled1_war_exploded/user\":\"已被使用\"}";
        }else{
//            isUsed="<h1>用户名已不存在！</h1>";
           // used = false;b
            jsonData="{\"http://localhost:8080/untitled1_war_exploded/user\":\"未被使用\"}";
        }
        PrintWriter out = response.getWriter();
        out.print(jsonData);//把结果响应到页面中
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
